package com.zy.openchat.core.model.circle;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.zy.openchat.core.model.user.UserInfo;

public class BlogUnreadInfo implements Parcelable {

    @SerializedName(value = "@type")
    public String type;
    public InputBlogIdType id;
    public BlogUserDateInfo user_date;
    public BlogInfo blog;
    public BlogReplyInfo reply;

    public long findUserId(){
        if(reply!=null){
            return reply.user_id;
        }
        if(user_date!=null){
            return user_date.user_id;
        }
        return 0;
    }

    public boolean isMessageEqual(BlogUnreadInfo blogInfo){
        if(blogInfo==null){
            return false;
        }
        return this.equals(blogInfo);
    }

    public boolean isAvaterDownloaded = false;

    public boolean isAvaterDownloadEqual(BlogUnreadInfo fullInfo){
        long tUserId = findUserId();
        long fUserId = fullInfo.findUserId();
        if(tUserId==0||fUserId==0){
            return false;
        }
        UserInfo oldUserInfo = UserInfo.getUserInfo(tUserId);
        UserInfo newUserInfo = UserInfo.getUserInfo(fUserId);
        if(oldUserInfo==null&&newUserInfo==null){
            return true;
        }
        if(oldUserInfo==null||newUserInfo==null){
            return false;
        }
        boolean newIsAvaterDownloaded = false;
        if(newUserInfo.profile_photo!=null&&newUserInfo.profile_photo.isSmallPhotoDownloaded()){
            newIsAvaterDownloaded = true;
        }
        return newIsAvaterDownloaded == isAvaterDownloaded;
    }



    public boolean isReplyUserEqual(BlogUnreadInfo fullInfo){
        if(fullInfo==null||fullInfo.reply==null||reply==null){
            return false;
        }
        if(reply.reply_user_id<=0||fullInfo.reply.reply_user_id<=0){
            return false;
        }
        UserInfo oldUserInfo = UserInfo.getUserInfo(reply.reply_user_id);
        UserInfo newUserInfo = UserInfo.getUserInfo(fullInfo.reply.reply_user_id);
        if(oldUserInfo==null&&newUserInfo==null){
            return true;
        }
        return false;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.type);
        dest.writeParcelable(this.id, flags);
        dest.writeParcelable(this.user_date, flags);
        dest.writeParcelable(this.reply, flags);
    }

    public void readFromParcel(Parcel source) {
        this.type = source.readString();
        this.id = source.readParcelable(InputBlogIdType.class.getClassLoader());
        this.user_date = source.readParcelable(BlogUserDateInfo.class.getClassLoader());
        this.reply = source.readParcelable(BlogReplyInfo.class.getClassLoader());
    }

    public BlogUnreadInfo() {
    }

    protected BlogUnreadInfo(Parcel in) {
        this.type = in.readString();
        this.id = in.readParcelable(InputBlogIdType.class.getClassLoader());
        this.user_date = in.readParcelable(BlogUserDateInfo.class.getClassLoader());
        this.reply = in.readParcelable(BlogReplyInfo.class.getClassLoader());
    }

    public static final Creator<BlogUnreadInfo> CREATOR = new Creator<BlogUnreadInfo>() {
        @Override
        public BlogUnreadInfo createFromParcel(Parcel source) {
            return new BlogUnreadInfo(source);
        }

        @Override
        public BlogUnreadInfo[] newArray(int size) {
            return new BlogUnreadInfo[size];
        }
    };
}
